package com.lemon.smartsafety.ui.adapter.common;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.entity.common.SelectDataI;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/24.
 */
public class SelectAdapter extends LemonRcvAdapter<SelectDataI> {
    private RecyclerTClickListener<SelectDataI> mClickListener;
    private LoggerHelper loggerHelper = new LoggerHelper(SelectAdapter.class);

    private String  mOldValue        = null;
    private boolean showDownSubTitle = false;

    private List<SelectDataI> mSelectedItems = null;


    private boolean isMultiSelect = false;

    private SelectAdapter(@Nullable List data) {
        super(data);
    }

    public SelectAdapter(@Nullable List data, RecyclerTClickListener<SelectDataI> listener, String oldValue) {
        super(data);
        this.mClickListener = listener;
        this.mOldValue = oldValue;
        this.showDownSubTitle = false;
    }

    public SelectAdapter(@Nullable List data, RecyclerTClickListener<SelectDataI> listener, String oldValue, boolean isShowDownSubTitle) {
        super(data);
        this.mClickListener = listener;
        this.mOldValue = oldValue;
        this.showDownSubTitle = isShowDownSubTitle;
    }


    public SelectAdapter(@Nullable List data, RecyclerTClickListener<SelectDataI> listener, List<SelectDataI> oldItems) {
        super(data);
        this.mClickListener = listener;
        this.mOldValue = "";
        this.showDownSubTitle = false;
        this.mSelectedItems = oldItems;
        isMultiSelect = true;
        if (oldItems != null && oldItems.size() != 0 && mDataList.size()!=0) {
            initSelectedValue();
        }
    }

    public void initSelectedValue() {
        if (mSelectedItems != null && mSelectedItems.size() != 0) {
            for (int i = 0; mDataList != null && i < mDataList.size(); i++) {
                SelectDataI dataMust = mDataList.get(i);
                for (int j = 0; mSelectedItems != null && j < mSelectedItems.size(); j++) {
                    SelectDataI dataOld = mSelectedItems.get(j);
                    if (dataMust.getValue().equals(dataOld.getValue())) {
                        mDataList.get(i).setSelectedI(true);
                    }
                }
            }
        }
    }

    public SelectDataI getItemByPosition(int position) {
        if (position >= mDataList.size() || position < 0) {
            return null;
        }
        return mDataList.get(position);
    }



    public SelectDataI setSelectedByPosition(int position, boolean sel) {
        mDataList.get(position).setSelectedI(sel);
        SelectDataI data = mDataList.get(position);
        notifyDataSetChanged();
        return data;
    }

    public SelectDataI setSelectedByPositionSingle(int position) {
        for (int i = 0; i < mDataList.size(); i++) {
            mDataList.get(i).setSelectedI(false);
        }
        mDataList.get(position).setSelectedI(true);
        SelectDataI data = mDataList.get(position);
        this.mOldValue = data.getValue();
        notifyDataSetChanged();
        return data;
    }



    public SelectDataI getSelectedSingle() {
        SelectDataI data = null;
        for (int i = 0; i < mDataList.size(); i++) {
            SelectDataI temp = mDataList.get(i);
            if (temp.isSelectedI()) {
                data = temp;
                break;
            }
        }
        return data;
    }

    @Override
    public Object getItemType(SelectDataI SelectDataI) {
        return -1;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object obj) {
        return new SelectAdapterItem(this.mOldValue, this.mClickListener, this.showDownSubTitle);
    }


    /**
     * Created by Cooliean on 16/3/24.
     */
    public class SelectAdapterItem implements AdapterItem<SelectDataI> {
        private RecyclerTClickListener<SelectDataI> mClickListener;

        private SelectDataI mData;
        private int         mPosition;
        private String  mOldValue        = null;
        private boolean showDownSubTitle = false;


        private LinearLayout   ripple;
        private LinearLayout drawer;
        private TextView     itemText;
        private TextView     itemSubDownText;
        private TextView     itemSubText;
        private ImageView    imageSelect;


        public SelectAdapterItem(String oldValue, RecyclerTClickListener<SelectDataI> clickListener) {
            this.mClickListener = clickListener;
            this.mOldValue = oldValue;
            this.showDownSubTitle = false;
        }

        public SelectAdapterItem(String oldValue, RecyclerTClickListener<SelectDataI> clickListener, boolean isShowDownSubTitle) {
            this.mClickListener = clickListener;
            this.mOldValue = oldValue;
            this.showDownSubTitle = isShowDownSubTitle;
        }


        @Override
        public int getLayoutResId() {
            return R.layout.item_select;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            itemText = (TextView) root.findViewById(R.id.itemText);
            itemSubText = (TextView) root.findViewById(R.id.itemSubText);
            itemSubDownText = (TextView) root.findViewById(R.id.itemSubDownText);
            imageSelect = (ImageView) root.findViewById(R.id.imageSelect);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(mData, mPosition);
                }
            });
          /*  ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    mClickListener.onItemClick(mData, mPosition);
                }
            });*/
        }

        @Override
        public void handleData(SelectDataI data, int position) {
            mData = data;
            mPosition = position;
            itemText.setText(data.getTitle());
            if (!isMultiSelect) {
                if ((StringUtils.nullValue(data.getValue()).length() != 0 && StringUtils.nullValue(data.getValue())
                                                                                       .equals(this.mOldValue)) || data.isSelectedI()) {
                    imageSelect.setImageDrawable(imageSelect.getContext()
                                                            .getResources()
                                                            .getDrawable(R.mipmap.ic_item_selected));
                } else {
                    imageSelect.setImageDrawable(imageSelect.getContext()
                                                            .getResources()
                                                            .getDrawable(R.mipmap.ic_item_normal));
                }
            } else {//多选处理
                if (data.isSelectedI()) {
                    imageSelect.setImageDrawable(imageSelect.getContext()
                                                            .getResources()
                                                            .getDrawable(R.mipmap.ic_item_selected));
                } else {
                    imageSelect.setImageDrawable(imageSelect.getContext()
                                                            .getResources()
                                                            .getDrawable(R.mipmap.ic_item_normal));
                }
            }

            if (this.showDownSubTitle) {
                itemSubDownText.setText(StringUtils.nullValue(data.getOther().toString()));
                itemSubDownText.setVisibility(View.VISIBLE);
            } else {
                itemSubDownText.setVisibility(View.GONE);
            }

        }
    }

}
